<?php
declare (strict_types = 1);

namespace app\controller;

use app\model\Months;
use think\facade\Validate;
use think\Request;

class Month
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index( Months $months)
    {
        //调用模型层实现展示
        $res=$months->getAll();
        //判断是否展示成功 返回数据
        if($res){
            return json(['code'=>0,'msg'=>'展示成功','data'=>$res]);
        }else{
            return json(['code'=>1,'msg'=>'展示失败','data'=>'']);
        }
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request,Months $months)
    {
        //接收字段
        $data['goods_name']=$request->param('goods_name');
        $data['goods_type']=$request->param('goods_type');
        $data['store_count']=$request->param('store_count');
        $data['is_show']=$request->param('is_show');
        $data['is_hot']=$request->param('is_hot');
        //验证规则
        $rule = [
        'goods_name'  => 'require|max:32',
        'goods_type'   => 'require'
    ];

        $message = [
        'goods_name.require' => '名称必须',
        'name.max'     => '名称最多不能超过32个字符',
        'goods_type.require'   => '类别必填',
    ];
        $validate=Validate::rule($rule)->message($message);
        if(!$validate->check($data)){
            return json(['code'=>1,'msg'=>$validate->getError(),'data'=>'']);
        }
        //调用模型层
        $res=$months->addOne($data);
        //判断是否添加成功 返回数据
        if($res){
            return json(['code'=>0,'msg'=>'添加成功','data'=>$data]);
        }else{
            return json(['code'=>1,'msg'=>'添加失败','data'=>'']);
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id, Months $months)
    {

        //接收字段
        $where['goods_id']=$id;
        $data['goods_name']=$request->param('goods_name');
        $data['goods_type']=$request->param('goods_type');
        $data['store_count']=$request->param('store_count');
        $data['is_show']=$request->param('is_show');
        $data['is_hot']=$request->param('is_hot');
        //验证规则
        $rule = [
            'goods_name'  => 'require|max:32',
            'goods_type'   => 'require'
        ];

        $message = [
            'goods_name.require' => '名称必须',
            'name.max'     => '名称最多不能超过32个字符',
            'goods_type.require'   => '类别必填',
        ];
        $validate=Validate::rule($rule)->message($message);
        if(!$validate->check($data)){
            return json(['code'=>1,'msg'=>$validate->getError(),'data'=>'']);
        }
        //调用模型层
        $res=$months->upOne($where,$data);
        //判断是否修改成功 返回数据
        if($res){
            return json(['code'=>0,'msg'=>'修改成功','data'=>$data]);
        }else{
            return json(['code'=>1,'msg'=>'修改失败','data'=>'']);
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id,Months $months)
    {
        //接收ID
        $where['goods_id']=$id;
        $res=$months->delOne($where);
        //判断是否删除成功 返回数据
        if($res){
            return json(['code'=>0,'msg'=>'删除成功','data'=>$res]);
        }else{
            return json(['code'=>1,'msg'=>'删除失败','data'=>'']);
        }
    }
}
